function [lon,lat,data,time_sec,time_txt,deptht]=rnemo_3d_selec(bbio,bbio_dir,gridtype,kind,levmin,levmax,ii,jj,year,month,day)

% ---- Read NEMO 3D model output on the whole simulation (1997-2000):
% Input: 
%  - bbio : Configuration
%  - bbio_dir : directory where the outputs are stored
%  - gridtype : kind of file to open ('gridT', 'gridU', 'gridW', 'gridV', 'ptrcT',
%  'advtrcT')
%  - kind : data that would be extracted
%           - 'T' : Temperature
%           - 'S' : Salinity
%           - 'phy' : Phytoplankton
%           - 'zoo' : Zooplankton
%           - 'det' : Detritus
%           - 'no3' : Nitrates
%           - 'don' : DON
%           - 'PP'  : Primary Production
%           - 'zle'  : Ligh limitation
%           - 'zlno3'  : Nutrient limitation
%           - 'u'   : Zonal velocity
%           - 'v'   : Meridional velocity
%           - 'w' : Vertical velocity
%
%  - levmin : First vertical level to read
%  - levmax : Last vertical level to read
%  - ii: indices (from [i,j]=find(...) ) of longitudes to read (the program
%        add one point after the last and before the first longitude)
%  - jj: indices (from [i,j]=find(...) ) of latitudes to read (the program 
%        add one point after the last and before the first longitude)
%  - year : Year to read (i.e.: 1998, ...) OR Tag date (i.e.: 'y1997_FALL')
%  - month : Month to read (i.e.: 1, 2, 3, ..., 12) - enter only one month
%  - day : Day to read (i.e.: 1, 31, ...) - enter only one day
%
%
% G. Charria 09/2008
% --------------------------------------------------
switch nargin
    case {5,6,7,8,9,10,11}
    
    otherwise
        disp('Wrong number of arguments'); 
        lon=[];
        lat=[];
        data=[];
        time_sec=[];
        time_txt=[];
        deptht=[];
        return
end

if (strcmp(kind,'T')==1)
    ik=5;
elseif (strcmp(kind,'S')==1)
    ik=6;
elseif (strcmp(kind,'phy')==1)
    ik=6;
elseif (strcmp(kind,'zoo')==1)
    ik=5;
elseif (strcmp(kind,'det')==1)
    ik=8;
elseif (strcmp(kind,'no3')==1)
    ik=7;
elseif (strcmp(kind,'don')==1)
    ik=9;
elseif (strcmp(kind,'PP')==1)
    ik=10;
elseif (strcmp(kind,'zle')==1)
    ik=12;
elseif (strcmp(kind,'zlno3')==1)
    ik=13; 
elseif (strcmp(kind,'u')==1)
    ik=5;
elseif (strcmp(kind,'v')==1)
    ik=5; 
elseif (strcmp(kind,'w')==1)
    ik=5;
end

disp('Reading Netcdf files ...')

if (nargin == 9)
    if isnumeric(year)
        [status,liste] = unix(['ls -al ',bbio_dir,'*',bbio,'*y',num2str(year),'*',gridtype,'.nc ']);
    else
        [status,liste] = unix(['ls -al ',bbio_dir,'*',bbio,'*',year,'*',gridtype,'.nc ']);
    end     
elseif (nargin == 10)
    [status,liste] = unix(['ls -al ',bbio_dir,'*',bbio,'*y',num2str(year),'m',num2str(month,'%02.0f'),'*',gridtype,'.nc ']);     
elseif (nargin == 11)
    [status,liste] = unix(['ls -al ',bbio_dir,'*',bbio,'*y',num2str(year),'m',num2str(month,'%02.0f'),'d',num2str(day,'%02.0f'),'_',gridtype,'.nc ']);
else
    [status,liste] = unix(['ls -al ',bbio_dir,'*',bbio,'*',gridtype,'.nc ']);
end
   
% Indice de reperage pour les dates de chaque fichier
txt=[bbio,'_'];
inddate=findstr(liste,txt);


time_sec(1:size(inddate,2))=NaN;
data(1:levmax-levmin+1,1:(max(ii)+1)-(min(ii)-1)+1,1:(max(jj)+1)-(min(jj)-1)+1,1:size(inddate,2))=NaN;
time_txt(1:size(inddate,2),11)=NaN;

h=waitbar(0,'Loading modelled fields ...');
for id=1:size(inddate,2)    
    waitbar(id/size(inddate,2)) 
    datet1=liste(inddate(id)+size(txt,2):inddate(id)+size(txt,2)+10);
    filename=['NATL4-',bbio,'_',datet1,'_',gridtype,'.nc'];
    % --- NetCDF file loading ...
    nc = netcdf([bbio_dir,filename],'nowrite');     % Open NetCDF file.
    %description = att(nc);                            % Global attributes.
    variables = var(nc);
    %names_var = ncnames(variables)                   % Names of Variables
    
    time_raw=variables{4}(:);
    deptht=variables{3}(:);
    lat=variables{2}(min(ii)-1:max(ii)+1,min(jj)-1:max(jj)+1);
    lon=variables{1}(min(ii)-1:max(ii)+1,min(jj)-1:max(jj)+1);
    data_raw=variables{ik}(:,levmin:levmax,min(ii)-1:max(ii)+1,min(jj)-1:max(jj)+1); 


    close(nc);
   
    time_sec(id)=time_raw;
    data(:,:,:,id)=squeeze(data_raw);
   
    time_txt(id,:)=datet1;
   
  
    data_raw=[];
    clear data_raw
end % end id
close(h)

disp('Done !')


